#!/bin/bash

#/bin/cp /home/sioban/.mozilla/firefox-3.5/2gufwxr0.default/cookies.sqlite .
#session=$(sqlite3 -line cookies.sqlite 'select value from moz_cookies where name = "_undead_session" order by expiry DESC;' | head -1 | sed "s/value = //")
session="$(cat cookie)"

ifile=codes_undeadlive.txt
# ifile=invite.list

grep "^[0-9]" $ifile | head -50 > codes

cat codes | sed "s/[^0-9]//" | while read code 
do
			# -H "Accept-Encoding: gzip,deflate" \

	return="$(curl -s -d "user_code=$code" \
			-H "Host: www.undeadlive.com" \
			-H "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2" \
			-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
			-H "Accept-Language: en-us,en;q=0.5" \
			-H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" \
			-H "Keep-Alive: 300" \
			-H "Connection: keep-alive" \
			-H "Content-Type: application/x-www-form-urlencoded" \
			-H "Referer: http://www.undeadlive.com/network" \
			-H "Content-Length: 19" \
			-H "Cookie: _undead_session=$session; SERVERID=undead_j2" \
		http://www.undeadlive.com/network/send_invite )"

        if [ "$(echo "$return" | grep "http://www.undeadlive.com/user/relogin")" ]        
        then    
                echo "you must reset the session cookie"
        	exit -1 
        fi      

			# -H "Accept-Encoding: gzip,deflate" \

	return="$(curl -s -H "Host: www.undeadlive.com" \
			-H "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2" \
			-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
			-H "Accept-Language: en-us,en;q=0.5" \
			-H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" \
			-H "Keep-Alive: 300" \
			-H "Connection: keep-alive" \
			-H "Cookie: _undead_session=$session; SERVERID=undead_j2" \
		http://www.undeadlive.com/network | sed "s@<b>@@" | sed "s@</b>@@" | sed "s@.*\(error\|notice\)-box\(.*\)div>.*@\2@p;d" | sed "s@.*lead[^>]*>\([^:]*\):</span> \([^<]*\)<.*@\1: \2@")"

	echo "$code = $return"

	if [ "$(echo "$return" | grep "maximum allowed")" ] 
	then
		exit -1
	else
		sed "s/$code//" $ifile > $ifile.sed
		mv $ifile.sed $ifile
	fi
done
